package com.ym.b2b.product.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.ym.b2b.commons.exception.BaseException;
import com.ym.b2b.commons.service.BaseService;
import com.ym.b2b.product.dao.EmBProductTypeDAO;
import com.ym.b2b.product.service.EmBProductTypeService;
import com.ym.b2b.product.sqlbean.EmBProductType;
import com.zc.commons.base.Result;
import com.zc.commons.util.ZcStringUtil;
import com.zxjlib.core.webui.easyui.datamodel.ComboTreeDataModel;
import com.zxjlib.core.webui.easyui.datamodel.TreeGridDataModel;

@Service("emBProductTypeService")
public class EmBProductTypeServiceImpl extends BaseService implements EmBProductTypeService {

    @Autowired
    private EmBProductTypeDAO emBProductTypeDAO;

    @Override
    public List<EmBProductType> searchEmBProductType(EmBProductType emBProductType) throws BaseException {

        return searchAllType(emBProductType);
    }

    /**
     * 全部分类
     */

    @Cacheable(value = "product", key = "#emBProductType.cacheKey", condition = "#emBProductType.searchFlag eq 'tag'")
    private List<EmBProductType> searchAllType(EmBProductType emBProductType) {

        // 递归分类
        Integer l = emBProductTypeDAO.searchTypeLevel();
        emBProductType.setTypeLevel(1);
        List<EmBProductType> list1 = emBProductTypeDAO.searchEmBProductType(emBProductType);

        if (CollectionUtils.isNotEmpty(list1)) {
            multiplyType(list1, emBProductType, 2, l);
        }
        return list1;
    }

    /**
     * 递归分类
     */
    private void multiplyType(List<EmBProductType> l, EmBProductType emBProductType, Integer beginLevel,
                              Integer topLevel) {
        if (beginLevel > topLevel) {
            return;
        } else {
            emBProductType.setTypeLevel(beginLevel);
            List<EmBProductType> list1 = emBProductTypeDAO.searchEmBProductType(emBProductType);
            for (EmBProductType t1 : l) {

                if (beginLevel == 2) {
                    t1.set_parentId("");
                }
                if (CollectionUtils.isNotEmpty(list1)) {
                    for (EmBProductType t2 : list1) {
                        if (t2.getParentTypeId().longValue() == t1.getProductTypeId().longValue()) {
                            t1.getEmBProductTypeList().add(t2);
                        }
                    }
                }
            }
            beginLevel++;
            multiplyType(list1, emBProductType, beginLevel, topLevel);
        }

    }

    /**
     * 返回 comboxTree 样式的列表 老方法 待改
     */
    private List<ComboTreeDataModel> getComboTreeType(EmBProductType emBProductType) {
        ComboTreeDataModel m = new ComboTreeDataModel();
        List<ComboTreeDataModel> l = new ArrayList<ComboTreeDataModel>();
        List<ComboTreeDataModel> rl = new ArrayList<ComboTreeDataModel>();
        List<EmBProductType> list = searchAllType(emBProductType);

        for (EmBProductType e : list) {
            ComboTreeDataModel c = new ComboTreeDataModel();
            c.setId(e.getProductTypeId().toString());
            c.setText(e.getProductTypeName());
            c.setCode(e.getProductTypeCode());
            if (CollectionUtils.isNotEmpty(e.getEmBProductTypeList())) {
                for (EmBProductType e1 : e.getEmBProductTypeList()) {
                    ComboTreeDataModel c1 = new ComboTreeDataModel();
                    c1.setId(e1.getProductTypeId().toString());
                    c1.setText(e1.getProductTypeName());
                    c1.setCode(e1.getProductTypeCode());
                    if (CollectionUtils.isNotEmpty(e1.getEmBProductTypeList())) {
                        for (EmBProductType e2 : e1.getEmBProductTypeList()) {
                            ComboTreeDataModel c2 = new ComboTreeDataModel();
                            c2.setId(e2.getProductTypeId().toString());
                            c2.setText(e2.getProductTypeName());
                            c2.setCode(e2.getProductTypeCode());

                            if (CollectionUtils.isNotEmpty(e2.getEmBProductTypeList())) {
                                for (EmBProductType e3 : e2.getEmBProductTypeList()) {
                                    ComboTreeDataModel c3 = new ComboTreeDataModel();
                                    c3.setId(e3.getProductTypeId().toString());
                                    c3.setText(e3.getProductTypeName());
                                    c3.setCode(e3.getProductTypeCode());

                                    if (CollectionUtils.isNotEmpty(e3.getEmBProductTypeList())) {
                                        for (EmBProductType e4 : e3.getEmBProductTypeList()) {
                                            ComboTreeDataModel c4 = new ComboTreeDataModel();
                                            c4.setId(e4.getProductTypeId().toString());
                                            c4.setText(e4.getProductTypeName());
                                            c4.setCode(e4.getProductTypeCode());

                                            c3.getChildren().add(c4);
                                        }

                                    } else {
                                        c3.setState("close");
                                    }

                                    c2.getChildren().add(c3);
                                }

                            } else {
                                c2.setState("close");
                            }
                            c1.getChildren().add(c2);
                        }
                    } else {
                        c1.setState("close");
                    }
                    c.getChildren().add(c1);
                }
            } else {
                c.setState("close");
            }
            l.add(c);
        }
        m.setId("0");
        m.setState("open");
        m.setText("商品分类根目录");
        rl.addAll(l);
        return rl;
    }

    /**
     * 返回 TreeGrid 样式的列表
     */
    private List<TreeGridDataModel> getTreeGridType(EmBProductType emBProductType) {

        return null;
    }

    private void hasTheRootEmBProductType(EmBProductType emBProductType, List<EmBProductType> list) {
        if (null != emBProductType.getParentTypeId() && !"".equals(emBProductType.getParentTypeId())) {
            // 检查是否为根目录
            Long a = emBProductTypeDAO.hasTheRootEmBProductType(emBProductType);
            if (a == 0) {
                for (EmBProductType t : list) {
                    t.setState("");
                }
            }
        }
    }

    @Override
    public Result saveOrUpdateEmBProductType(EmBProductType emBProductType) throws BaseException {
        // check
        Result ret = checkInputProductType(emBProductType);
        emBProductType.setTypeLevel(22222);// 随便填写一个值

        if (null == emBProductType.getParentTypeId() || emBProductType.getParentTypeId() == 0) {
            emBProductType.setParentTypeId(null);
        }
        if (null != emBProductType.getProductTypeId() && !"".equals(emBProductType.getProductTypeId())) {
            // update
            try {
                emBProductTypeDAO.updateEmBProductType(emBProductType);
            } catch (Exception e) {
                logger.error(e.getMessage(), e);
                throw new BaseException("修改分类失败");
            }
        } else {
            // insert
            try {
                emBProductTypeDAO.insertEmBProductType(emBProductType);
            } catch (Exception e) {
                logger.error(e.getMessage(), e);
                throw new BaseException("新增分类失败");
            }
        }

        return ret;
    }

    private Result checkInputProductType(EmBProductType emBProductType) {
        Result ret = new Result();
        if (ZcStringUtil.isBlank(emBProductType.getProductTypeName())) {
            ret.setCodeResult(Result.CODE_100, "分类名称不能为空!");
            return ret;
        }
        return ret;
    }

    @Transactional
    @Override
    public void deleteEmBProductType(EmBProductType emBProductType) throws BaseException {

        if (null == emBProductType.getProductTypeId() || "".equals(emBProductType.getProductTypeId())) {
            throw new BaseException("缺少商品分类 主键ID");
        }
        try {
            emBProductTypeDAO.deleteEmBProductType(emBProductType);
            EmBProductType emBProductType2 = new EmBProductType();
            emBProductType2.setParentTypeId(emBProductType.getProductTypeId());
            emBProductTypeDAO.deleteEmBProductType(emBProductType2);
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            throw new BaseException("删除分类失败");
        }

    }

    @Override
    public List<EmBProductType> searchLevelEmBProductType(EmBProductType emBProductType) throws BaseException {
        // 判断是否还有下一级

        return emBProductTypeDAO.searchEmBProductType(emBProductType);
    }

    @Override
    public Object showEmBproductTypeList(EmBProductType emBProductType) throws BaseException {

        if (ZcStringUtil.isBlank(emBProductType.getViewType())) {

            return searchAllType(emBProductType);

        } else if (emBProductType.getViewType().equals("treeGrid")) {

            return getTreeGridType(emBProductType);

        } else if (emBProductType.getViewType().equals("comboTree")) {

            return getComboTreeType(emBProductType);

        } else {
            return null;
        }
    }
}
